<template>
  <div class="app-container">
    <!-- 模糊查询-->

    <el-form size="small" :inline="true" label-width="68px">
      <el-form-item label="选择项目" prop="projectId">
        <el-select v-model="selectValue" placeholder="请选择" @change="selectChange">
          <el-option v-for="item in options" :key="item.projectId" :label="item.projectName" :value="item.projectId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="2.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd">新增主题
        </el-button>
      </el-col>
    </el-row>

    <div class="table-container" v-for="monthImage in monthImageList">
      <el-container >
        <el-header>
          <!--        表格头-->
          <el-row :gutter="20">
            <!--          主题名-->
            <el-col :span="4" :min-width=100>
              <span>主题: {{ monthImage.imageTitle }} </span>
            </el-col>
            <!--          详情标签-->
            <el-col :span="1" :min-width=100>
              <el-popover placement="top-start" title="主题详情" width="200" trigger="hover">
                <span>月度形象图主键: {{ monthImage.imageId }}</span><br>
                <span>项目主键: {{ monthImage.projectId }}</span><br>
                <span>月度形象图标题: {{ monthImage.imageTitle }}</span><br>
                <span>详情:{{ monthImage.imageDatil }}</span><br>
                <span>数据更新时间:{{ monthImage.updateDate.substring(0, 10) }}</span>
                <el-button slot="reference" type="text" style="font-size: 16px;padding: 5px">详情</el-button>
              </el-popover>
            </el-col>
            <el-col :span="2" :offset="1" :min-width="100">
              <el-button type="primary" plain icon="el-icon-edit" size="mini"
                         @click="handleUpdate(monthImage.imageId)" align="right">修改主题
              </el-button>
            </el-col>
            <el-col :span="2" :min-width="100">
              <el-button type="danger" plain icon="el-icon-delete" size="mini"
                         @click="handleDelete(monthImage.imageId)" align="right">删除主题
              </el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-container>
          <!--        主题效果图-->
          <el-aside width="200px" >
            <span>效果图:</span>
            <div>
              <ImagePreview :src="monthImage.effectSrc"></ImagePreview>
            </div>
          </el-aside>
          <!--        月度详情图-->
          <div style="width: 100%;overflow-x: auto;white-space: nowrap;display: flex; padding: 5px">
            <div class="detail" align="center" v-for="(item,index) in monthImage.details">
              <div>
                <span>{{ item.imageDetailDate.substring(0, 7) }}</span><br>
              </div>
              <div style="width: 160px;height: 160px;padding: 5px ">
                <ImagePreview :src="item.imageDetailPic"></ImagePreview>
              </div>
              <div style="margin: 5px">
                <el-button type="primary" plain icon="el-icon-edit" size="mini"
                           @click="handleUpdateDetail(item.imageDetailId,monthImage.imageTitle)">修改月图
                </el-button>
                <el-button type="danger" plain icon="el-icon-delete" size="mini"
                           @click="handleDeleteDetail(item.imageDetailId)">删除月图
                </el-button>
              </div>
            </div>
          </div>
          <div class="detail" align="center" style="padding-left: 10px;padding-right: 10px">
            <el-button
              plain
              @click="handleAddDetail(monthImage.imageId,monthImage.imageTitle)"
              style="margin-top:20px ">
              <i class="el-icon-plus" style="font-size: 100px"></i>
            </el-button>
          </div>
        </el-container>
      </el-container>
    </div>


    <!-- 添加或修改月度图对话框 -->
    <el-dialog :title="titleDetail" :visible.sync="openDetail" width="500px">
      <el-form ref="formDetail" :model="formDetail" :rules="rulesDetail" label-width="80px">
        <el-form-item label="所属主题" >
          <el-input v-model="detailName" readonly disabled/>
        </el-form-item>
        <el-form-item label="效果图" prop="imageDetailPic">
          <ImageUpload v-model="formDetail.imageDetailPic" :limit="1"></ImageUpload>
        </el-form-item>
        <el-form-item label="上传时间" prop="imageDetailDate">
          <el-date-picker clearable
                          v-model="formDetail.imageDetailDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改主题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目" >
          <el-input v-model="projectName" readonly disabled/>
        </el-form-item>
        <el-form-item label="月度形象图标题" prop="imageTitle">
          <el-input v-model="form.imageTitle" placeholder="请输入月度形象图标题"/>
        </el-form-item>
        <el-form-item label="详情" prop="imageDatil">
          <el-input v-model="form.imageDatil" placeholder="请输入详情"/>
        </el-form-item>
        <el-form-item label="效果图" prop="effectSrc">
          <ImageUpload v-model="form.effectSrc" :limit="1"></ImageUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMonthImage, getMonthImage, delMonthImage, addMonthImage, updateMonthImage} from "@/api/project/monthImage";
import {
  getMonthImageDetail,
  delMonthImageDetail,
  addMonthImageDetail,
  updateMonthImageDetail
} from "@/api/project/monthImageDetail";
import {listProjectByUserId} from "@/api/project/project";
import ImagePreview from "@/components/ImagePreview/index.vue";
import ImageUpload from "@/components/ImageUpload/index.vue";

export default {
  name: "MonthImage",
  components: {ImagePreview, ImageUpload},
  data() {
    return {
      // 主题名
      detailName: "",
      // 项目名称
      projectName: "",
      // 项目列表的数组
      options: [],
      // 选择的值
      selectValue: null,
      // 遮罩层
      loading: true,
      // 月度形象图表格数据
      monthImageList: [],
      // 弹出层标题
      title: "",
      // 弹出层标题
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
      },
      // 表单参数
      form: {
        imageTitle: null,
        imageDatil: null,
        effectSrc: null
      },
      // 表单校验
      rules: {
        imageTitle: [
          {required: true, message: "月度形象图标题不能为空", trigger: "blur"}
        ],
        imageDatil: [
          {required: true, message: "详情不能为空", trigger: "blur"}
        ],
        effectSrc: [
          {required: true, message: "效果图不能为空", trigger: "blur"}
        ]
      },
      // 表单参数
      formDetail: {
        imageDetailPic: null,
        imageDetaiDate: null,
      },
      // 表单校验
      rulesDetail: {
        imageDetailPic: [
          {required: true, message: "效果图不能为空", trigger: "blur"}
        ],
        imageDetailDate: [
          {required: true, message: "上传时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getListProject();
  },
  methods: {
    /** 用户能看见的项目列表
     * 这个方法只有页面创建时才会执行一次*/
    getListProject() {
      listProjectByUserId().then(response => {
        this.options = response.data;
        this.selectValue = this.options[0].projectId;
        // 向查询列表页添加项目id
        this.queryParams.projectId = this.selectValue
        // 给表单中项目名
        this.projectName = this.options.find(item => item.projectId === this.selectValue).projectName
        this.getList();
      });
    },
    selectChange() {
      // 向查询列表页添加项目id
      this.queryParams.projectId = this.selectValue
      // 给表单中添加不可更改的项目名
      this.projectName = this.options.find(item => item.projectId === this.selectValue).projectName
      this.getList();
    },
    /** 查询月度形象图列表 */
    getList() {
      this.loading = true;
      listMonthImage(this.queryParams).then(response => {
        this.monthImageList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
      this.resetDetail();
    },
    // 表单重置
    reset() {
      this.form = {
        imageId: null,
        projectId: null,
        imageTitle: null,
        imageDatil: null,
        updateDate: null,
        effectSrc: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetDetail() {
      this.formDetail = {
        imageDetailId: null,
        imageId: null,
        imageDetailPic: null,
        imageDetailDate: null,
      };
      this.resetForm("formDetail");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主题";
      this.form.projectId = this.selectValue;
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getMonthImage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.imageId != null) {
            updateMonthImage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMonthImage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal.confirm('是否确认删除主题编号为"' + id + '"的数据项？').then(function () {
        return delMonthImage(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleAddDetail(id,detailName) {
      this.resetDetail();
      this.openDetail = true;
      this.titleDetail = "添加月度图";
      this.detailName = detailName;
      this.formDetail.imageId = id;
    },
    handleUpdateDetail(id,detailName) {
      this.resetDetail();
      this.detailName = detailName;
      this.formDetail.imageId = id;
      getMonthImageDetail(id).then(response => {
        this.formDetail = response.data;
        this.openDetail = true;
        this.titleDetail = "修改主题";
      });
    },
    submitFormDetail() {
      this.$refs["formDetail"].validate(valid => {
        if (valid) {
          if (this.formDetail.imageDetailId != null) {
            updateMonthImageDetail(this.formDetail).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openDetail = false;
              this.getList();
            });
          } else {
            addMonthImageDetail(this.formDetail).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openDetail = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDeleteDetail(id) {
      this.$modal.confirm('是否确认删除月度图编号为"' + id + '"的数据项？').then(function () {
        return delMonthImageDetail(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
<style>
.detail {
  display: inline-table;
  padding-top: 10px;
}

.el-header, .el-footer {

  background-color: #eeeeee;
  padding-top: 15px;
}

.el-aside {
  background-color: #ffffff;
  padding: 0 20px;

}

.el-main {
  background-color: #ffffff;

}

.el-container {
  margin-bottom: 5px;
}
.table-container{
  border: 1px dashed #ccc;
  border-radius: 5px;
  margin-bottom: 10px;
}
</style>
